package com.itheima.controller;



import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference//这个注解就能在zk查找这个服务了，一定是dubbo注解
    private RoleService roleService;


    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.bianji_OK);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.bianji_NOOK);
        }
    }


    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.ADD_juese_OK);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_juese_NOOK);
        }
    }


    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }




    //id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findByID(id);
            return new Result(true,MessageConstant.find_juese_OK,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.find_juese_NOOK);
        }
    }





    //根据角色ID查询权限选项
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.find_quanxian_OK,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.find_quanxian_NOOK);
        }

    }



    //根据角色id查询菜单选项
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> list = roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.find_juese_OK,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.find_juese_NOOK);
        }
    }



    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true,MessageConstant.del_juese_OK);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.del_juese_NOOK);
        }

    }

    //查询所有的角色
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> data = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }



}
